package com.zgh.init;

import com.zgh.manager.IManagerService;
import com.zgh.manager.module.HeartBeatInfo;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service("HeartBeatTask")
public class HeartBeatTask implements Job {

	@Autowired
	private IManagerService managerService;

	@Override
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		HeartBeatInfo heartBeatInfo=new HeartBeatInfo();

		Map<Thread, StackTraceElement[]> maps = Thread.getAllStackTraces();
		int totalThreadNumber=maps.keySet().size();  //线程数
		Runtime rt = Runtime.getRuntime();
		//long totalMemorySize = rt.totalMemory()/1024/1024; //初始的总内存
		long maxMemory = rt.maxMemory()/1024/1024; //最大可用内存
		long freeMemory = rt.freeMemory()/1024/1024; //当前可用内存

		heartBeatInfo.setTotalThreadNumber(totalThreadNumber);
		heartBeatInfo.setMaxMemory(maxMemory);
		heartBeatInfo.setFreeMemory(freeMemory);
		managerService.insertHeartBeatInfo(heartBeatInfo);
	}

}
